﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Controls.Ribbon;
using eStock.ChildWindows;
using eStock.Common;
using eStock.Controls;
using eStock.DataObjects;

namespace eStock
{
    partial class MainWindow
    {
        private void ExitClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void RibbonTabChanged(object sender, SelectionChangedEventArgs e)
        {
            RibbonTab tab = (RibbonTab)Ribbon.SelectedItem;
            switch(Convert.ToInt32(tab.Tag))
            {
                case 0:
                    if (DatabaseObjects.Components.Length == 0)
                        ContentPanel.Content = new TextBlock
                                                   {
                                                       Text = "Keine Bauteile gefunden. Fügen Sie neue Bauteile hinzu.",
                                                       VerticalAlignment = VerticalAlignment.Center,
                                                       HorizontalAlignment = HorizontalAlignment.Center,
                                                       FontSize = 16
                                                   };
                    else
                        ContentPanel.Content = _wndComponents;
                    break;
                case 1:
                    if (DatabaseObjects.Types.Length == 0)
                        ContentPanel.Content = new TextBlock
                        {
                            Text = "Keine Lager definiert. Bitte fügen Sie Lager hinzu.",
                            VerticalAlignment = VerticalAlignment.Center,
                            HorizontalAlignment = HorizontalAlignment.Center,
                            FontSize = 16
                        };
                    break;
                case 2:
                    if (DatabaseObjects.Trays.Length == 0)
                        ContentPanel.Content = new TextBlock
                        {
                            Text = "Keine Bauteiltypen gefunden. Fügen Sie neue Bauteiltypen hinzu.",
                            VerticalAlignment = VerticalAlignment.Center,
                            HorizontalAlignment = HorizontalAlignment.Center,
                            FontSize = 16
                        };
                    else
                        ContentPanel.Content = _wndComponentTypes;
                    break;
            }
        }


        private void RbAddComponentClick(object sender, RoutedEventArgs e)
        {
            WndAddEditComponent w = new WndAddEditComponent();
            w.ShowDialog();
        }


        private void RbEditClick(object sender, RoutedEventArgs e)
        {
            UIComponents ctrl = ContentPanel.Content as UIComponents;
            if (ctrl == null) return;
            Component item = ctrl.lbItems.SelectedItem as Component;
            if (item == null) return;

            WndAddEditComponent w = new WndAddEditComponent((int)item.Id);
            w.ShowDialog();
        }

        private void RbDeleteClick(object sender, RoutedEventArgs e)
        {
            UIComponents ctrl = ContentPanel.Content as UIComponents;
            if (ctrl == null) return;
            Component item = ctrl.lbItems.SelectedItem as Component;
            if (item == null) return;
            if (MessageBox.Show("Wollen Sie das ausgewählte Bauteil '" + item.Title + "' wirklich löschen?", "Bauteil löschen", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                DatabaseObjects.Components.Remove(item);
                DatabaseObjects.Components.Update();
                UpdateUI();
            }
        }

        private void SettingsClick(object sender, RoutedEventArgs e)
        {
            WndSettings wndSettings = new WndSettings();
            wndSettings.ShowDialog();
        }

        private void RbUpdateDbClick(object sender, RoutedEventArgs e)
        {
            Threading.RunAsThread((i, obj) => UpdateDatabase());
            Threading.WaitForThreadsAsnc(() => Dispatcher.Invoke(new Func<bool>(() => BusyIndi.IsBusy = true)),
                                         () => Dispatcher.Invoke(new Func<bool>(() => BusyIndi.IsBusy = false)));
        }

        private void RbPrintClick(object sender, RoutedEventArgs e)
        {
            //TODO
        }
    }
}
